<template>
  <div class="alarm-table">
    <el-table :data="alarmData" border style="width: 100%" max-height="400">
      <el-table-column prop="zig" label="拉出值" width="80">
        <template #default="scope">
          <span :class="getStatusClass(scope.row.status)">
            {{ scope.row.zig }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="hei" label="导高" width="80">
        <template #default="scope">
          <span :class="getStatusClass(scope.row.status)">
            {{ scope.row.hei }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template #default="scope">
          <el-tag :type="getTagType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="realpol" label="杆号" width="100" />
      <el-table-column prop="kmv" label="公里标" width="120" />
      <el-table-column prop="speed" label="速度" width="80">
        <template #default="scope">
          {{ scope.row.speed }} km/h
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="showPagination"
      v-model:current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next"
      @current-change="handlePageChange"
      style="margin-top: 10px; justify-content: center;"
    />

    <div v-if="showActions" class="table-actions">
      <el-button @click="handleReturnCurrent" type="primary" size="small">
        查看实时展示
      </el-button>
      <el-button @click="handleExport" size="small">
        导出数据
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import type { AlarmData } from '@/types/chart'

interface Props {
  alarmData: AlarmData[]
  total?: number
  pageSize?: number
  showPagination?: boolean
  showActions?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  total: 0,
  pageSize: 10,
  showPagination: true,
  showActions: true
})

const emit = defineEmits<{
  pageChange: [page: number]
  returnCurrent: []
  export: []
}>()

const currentPage = ref(1)

// 获取状态文本
const getStatusText = (status: number): string => {
  switch (status) {
    case 1:
      return '正常'
    case 2:
      return '预警'
    case 3:
      return '超限'
    default:
      return '未知'
  }
}

// 获取标签类型
const getTagType = (status: number): string => {
  switch (status) {
    case 1:
      return 'success'
    case 2:
      return 'warning'
    case 3:
      return 'danger'
    default:
      return 'info'
  }
}

// 获取状态样式类
const getStatusClass = (status: number): string => {
  switch (status) {
    case 1:
      return 'status-normal'
    case 2:
      return 'status-warning'
    case 3:
      return 'status-danger'
    default:
      return ''
  }
}

// 处理页码变化
const handlePageChange = (page: number) => {
  currentPage.value = page
  emit('pageChange', page)
}

// 处理返回实时展示
const handleReturnCurrent = () => {
  emit('returnCurrent')
}

// 处理导出
const handleExport = () => {
  emit('export')
}
</script>

<style scoped>
.alarm-table {
  padding: 10px;
}

.status-normal {
  color: #67c23a;
}

.status-warning {
  color: #e6a23c;
  font-weight: bold;
}

.status-danger {
  color: #f56c6c;
  font-weight: bold;
}

.table-actions {
  margin-top: 15px;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 10px;
}

.el-pagination {
  display: flex;
  justify-content: center;
}
</style>